package sz.szsmk.citizencard.util.zxyh;

import android.util.Base64;


import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;

/**
 * Copyright (c)2021/7/20
 * author: hxh
 * created on: 2021/7/20 11:02
 * description:
 */
public class SnowBallUtil {
    /**
     * SHA256WithRSA ----- SHA256WithRSA
     */
    public static String encrypt(String plainText) {
        try {
            Signature signature = Signature.getInstance("SHA256withRSA");//这个根据需求填充SHA1WithRSA或SHA256WithRSA
            PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.decode("MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCsWEpbBs05A5/l\n" +
                    "HU8GfbyEcuOPPeZgYns5zNlW+TB7s2T4u8unroqGLywi7O3WzUPEU+FKjobGZKMI\n" +
                    "W/PgUbDOicC4TiwAK612B9FdAl40aHIPTOAine7W1IvzVndXrmAm5nh0ubFUoPaz\n" +
                    "ylMQAaRxYuaFNzUHwIxUiB+CA9Hyo2TuHZ608OT0fNXy2WpOeZSGlQE7RfaDb53m\n" +
                    "URrT5y99LB5lch0IXgti+A1jj3uPjs2sk82ABnETnhoWLrYemnNUj152nmxwXvec\n" +
                    "Mw2N9/VyQuTLi1oIVPkoz9KoQNWrZYjclLb4r3t/DEN/YLEegwbRcVkgWiiI7Wbs\n" +
                    "Fj8CE+FJAgMBAAECggEAQ41316Ya2rYo/k02pHy0X9YIjCTfH/h6BHXe2tBhyJrQ\n" +
                    "gQ5bDSbE9UOYMThdIUzjuxiFvguPmEPqA2THeq2Kd+pWt+csKAT2wMTfs20yL6ig\n" +
                    "zI59bBQrgdld4cuKi7u1xImhvV7ruxg8msw7MpLyOd9iK+UvzBIU7y1L7FJDAK+V\n" +
                    "qKmsTeWLT0MOuqIUh01C7GpXOGO+XL6zWYfnMMSYRX5PtFJiWjQGWJj/4tgCdtyX\n" +
                    "tkPP/Opa+HEInEvjDxvfLNvXToJgTLjvUY9TwEPd1Lu1JRJ+fIbnUH2YtI88mZoP\n" +
                    "xsCiZMATVFBcrL1fEFxnpoFL0WeVzuVI4x46SrstgQKBgQDkfgWkk0+EInFycEe5\n" +
                    "fUkHpykND/tIxX8PgNyrl/YdX0HtoSFJRdQCNwAVsaEUoArBysL8fGcyVxcX/CAl\n" +
                    "pLhNGJe+4vgpCs9FewX99vCLC+ixdNOJiJYHkDj1zB9VPgPJfiICCI+WdI9LJ3oh\n" +
                    "+Slk6ovf16y6GMyVFnhNJQiuPQKBgQDBF9f8xEC9A/gddDXa0aZxWb+hOhdY/Qg7\n" +
                    "4K3AEc8EdoEjvss4P6U3hrQJRrNuhVSLQXOiXgIG0hfcGV/Pv6d8+AK+U9oY1QGB\n" +
                    "EsJVrFHRo5EIhKTN4Lf4I73qoYhqkvIyfC6HmZjq+gcgE2VJFuKZ+uXt0PmT2Dgu\n" +
                    "eNMl1dBb/QKBgGbTg8yVcqJfMbizaMPD6vqjsmireYU9Q7oBXs1l/J8pQC0lZypn\n" +
                    "X0qD3+jjYD886rpdejRP880GvmDP2NO39izT1W5VDCLEN5ToEUFCQOwCNvytO4lY\n" +
                    "S1QqBkgvTSZ6Ww/xUHKj0aJ+4AGNSXxCeettVfctQxEYmfC6BPeKpAZtAoGALA8u\n" +
                    "v4RgaUDAmqfrqfRwkCi0ayuALFQATvljbBIkNJZ9vRRJ9pm/Lz5TUlKJmsxdkDDt\n" +
                    "G71rpPbHbvdzP0oyA3fr2FX1y/i+kR8grL/XI4i5DUKHAFsoXzx4l1Mod3j0PsTL\n" +
                    "MTHBDMzkV1NVpynLpurzECLA8BVELIZxV47ELlkCgYAQaOW1FbaPcWFjwdQ0y60e\n" +
                    "7W/KjwmcGtgksyr0xBKwM73AdIjJ9dO48e5QXBOQU/XEquZVIVTau8lmSAp4K/32\n" +
                    "uE+ITvc5bFahTdhICmzk2fVO+q8o/c1Ma0rjrM7aErHZ091goodDcDTOz03LSQSH\n" +
                    "XphaeWm8dsk3uQEXOE4Uxw==", Base64.DEFAULT));
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
            signature.initSign(privateKey);
            signature.update(plainText.getBytes());
            byte[] result = signature.sign();
            char[] hexArray = "0123456789ABCDEF".toCharArray();
            char[] hexChars = new char[result.length * 2];
            for (int j = 0; j < result.length; j++) {
                int v = result[j] & 0xFF;
                hexChars[j * 2] = hexArray[v >>> 4];
                hexChars[j * 2 + 1] = hexArray[v & 0x0F];
            }
            return new String(hexChars);
        } catch (NoSuchAlgorithmException | InvalidKeyException | InvalidKeySpecException | SignatureException error) {
            String message = error instanceof NoSuchAlgorithmException ? "找不到参数签名的算法" : "签名密钥不可用";
            return "";
        }
    }
}
